// pages/myCenter/couponBagDetail.js
const controller = require("../controllers/Controller").controller

const controllerVip = require("../controllers/ControllerVip").controller

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    couponBagId: '',
    accountCouponBagId:'',
    orderNo: "",
    bagDetail: "",
    state: 0,
    selectTypeIndex: 0,
    effectiveTime: '',
    typeTabList: [{
        name: "优惠券"
      },
      {
        name: "会员卡"
      }
    ],
    stateList: [{
        name: "可用",
        state: 0,
        num: 0
      },
      {
        name: "历史",
        state: 1,
        num: 0
      },
      {
        name: "已赠送",
        state: 4,
        num: 0
      }
    ],
    selectIndex: 0,
    src1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756367236956160.png",
    src2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756346697449472.png",
    src3: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211203/916340952883265536.png",
    vip_bg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211126/913795093343764480.png",
  },

  //跳转会员卡详情页
  jumpToCardDetail(e) {
    let id = e.currentTarget.dataset.id;
    let shopId = e.currentTarget.dataset.shopid
    let url = "/pages/my/vip/my/VIPCardInfo?isShopCircleActive=1&id=" + id + "&shopId=" + shopId;
    wx.navigateTo({
      url: url,
    })
  },

  //获取tab数量
  // getTabNum(){
  //   let list=this.data.stateList
  //   let value1={}
  //   let value2={}
  //   let value3={}
  //   value1.couponBagId=this.data.couponBagId
  //   value1.orderNo=this.data.orderNo
  //   value1.shopId=this.data.shopId
  //   value2.couponBagId=this.data.couponBagId
  //   value2.orderNo=this.data.orderNo
  //   value2.shopId=this.data.shopId
  //   value3.couponBagId=this.data.couponBagId
  //   value3.orderNo=this.data.orderNo
  //   value3.shopId=this.data.shopId
  //   value1.state=0
  //   value2.state=1
  //   value3.state=4
  //   controller.getAccountBagDetail(value1).then(res=>{
  //       list[0].num=res.data.couponList.length
  //       this.setData({
  //         stateList:list
  //       })
  //   })
  //   controller.getAccountBagDetail(value2).then(res=>{
  //     list[1].num=res.data.couponList.length
  //     this.setData({
  //       stateList:list
  //     })
  //   })
  //   controller.getAccountBagDetail(value3).then(res=>{
  //     list[2].num=res.data.couponList.length
  //     this.setData({
  //       stateList:list
  //     })
  // })
  // },

  // 切换优惠券或者会员卡
  changeType(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectTypeIndex: index,
    })
  },

  //切换状态
  changeState(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      state: this.data.stateList[index].state
    })
    this.getAccountBagDetail()
  },

  //跳转优惠券详情
  toCouponDetail(e) {
    let id = e.currentTarget.dataset.id
    let couponAccountId = e.currentTarget.dataset.haveid
    let couponBagId = e.currentTarget.dataset.couponbagid
    let shopId = e.currentTarget.dataset.shopid

    wx.navigateTo({
      url: '/pages/couponBagCenter/couponDetail?id=' + id + "&couponAccountId=" + couponAccountId + '&couponBagId=' + couponBagId + "&out=1" + "&shopId=" + shopId,
    })

    // wx.requestSubscribeMessage({
    //   tmplIds:['DlEG_b9R6f2bWjrW-jEmsvhWwi5EwtF_vWG-pr3ZIds','DlEG_b9R6f2bWjrW-jEmsiB1UqeXvcEPjhoDA0azFXg'],
    //   success:res=>{
    //     console.log(res)
    //   },
    //   complete:()=>{
    //     wx.navigateTo({
    //       url: '/pages/couponBagCenter/couponDetail?id='+id+"&couponAccountId="+couponAccountId+'&couponBagId='+couponBagId+"&out=1"+"&shopId="+shopId,
    //     })
    //   }
    // })
  },


  //用户持有券详情 2.0
  async getAccountBagDetail() {
    let value = {}
    value.shopId = this.data.shopId
    value.state = this.data.state
    let res = {}
    if (this.data.orderNo) {
      value.orderNo = this.data.orderNo
      value.couponBagId = this.data.couponBagId
      res = await controller.getAccountBagDetail(value)
    } else {
      value.accountCouponBagId = this.data.accountCouponBagId
      res = await controller.couponBagDetail(value)
    }
    res.data.couponList.forEach(item => {
      if (item.timeState == 1) {
        item.effectiveStartTime = item.effectiveStartTime.split(" ")[0]
        item.effectiveEndTime = item.effectiveEndTime.split(" ")[0]
      }
    })

    this.setData({
      bagDetail: res.data
    })
  },


  //跳转店铺详情
  toShopDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/Index/Index?shopId=' + shopId,
    })
  },

  toUse(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    let haveId = e.currentTarget.dataset.haveid
    this.getCouponDetail(id, haveId)
    // wx.reLaunch({
    //   url: '/pages/Index/Index?shopId='+shopId,
    // })
  },

  //获取优惠券详情
  getCouponDetail(id, couponAccountId) {
    controller.shareCouponDetail({
      couponId: id
    }).then(res => {
      if (res.data.type == 6) {
        let goods = {}
        goods = res.data.goodsList[0]
        goods.shopId = res.data.shop.id
        goods.goodsId = res.data.goodsList[0].id
        goods.isPin = false
        goods.num = 1
        goods.couponAccountId = couponAccountId

        this.goodsSettlement(goods)
      } else {
        wx.navigateTo({
          url: '/pages/Index/Index?shopId=' + res.data.shop.id,
        })
      }
    })
  },

  goodsSettlement(goods) {
    wx.navigateTo({
      url: '/pages/cart/submitOrder/submitOrder?good=' + encodeURIComponent(JSON.stringify(goods)) + '&isGood=true' + "&shopId=" + goods.shopId + "&couponAccountId=" + goods.couponAccountId
    })
  },


  toDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    let couponAccountId = true
    console.log("chufa")
    wx.navigateTo({
      url: '/pages/couponBagCenter/couponDetail' + shopId + "&id=" + id,
    })
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      couponBagId: options.id,
      orderNo: options.orderNo,
      accountCouponBagId:options.accountCouponBagId
    })
    if (options.shopId) {
      this.setData({
        shopId: options.shopId,
      })
    }
  },
  onShow: function () {
    // this.getTabNum()
    this.getAccountBagDetail()
  },

})